package book.demo2;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class PlainOioServer {

    public void server(int port) throws IOException {
        final ServerSocket socket = new ServerSocket(port);

        for (;;){
            Socket clientSocket = socket.accept();
            System.out.println("Accepted connection from" + clientSocket);
            new Thread(()->{
                OutputStream out;
                try {
                    out = clientSocket.getOutputStream();
                    out.write("Hi\r\n".getBytes(StandardCharsets.UTF_8));
                }catch (IOException e){
                    e.printStackTrace();
                }
            }).start();
        }
    }

    public static void main(String[] args) throws Exception {
        new PlainOioServer().server(7788);
    }

}
